home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vg-2.03 / filelist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  2.8 KB  |  178 lines

  1. /*
  2.  * Copyright (C) 1990-1992 by Michael Davidson.
  3.  * All rights reserved.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software
  6.  * and its documentation for any purpose and without fee is hereby
  7.  * granted, provided that the above copyright notice appear in all
  8.  * copies and that both that copyright notice and this permission
  9.  * notice appear in supporting documentation.
  10.  *
  11.  * This software is provided "as is" without express or implied warranty.
  12.  */
  13.  
  14. /*
  15.  * file list handling
  16.  */
  17.  
  18. #include    <stdio.h>
  19. #include    <stdlib.h>
  20. #include    <dirent.h>
  21. #include    <string.h>
  22. #include    <sys/types.h>
  23. #include    <sys/stat.h>
  24.  
  25. #include    "vg.h"
  26.  
  27. STATIC list_t    *add_file(char *, list_t *);
  28. STATIC list_t    *add_dir(char *, list_t *);
  29.  
  30. list_t *
  31. fileListCreate(
  32.     char    **v
  33.     )
  34. {
  35.     list_t    *f;
  36.     char    *name;
  37.     struct stat    s;
  38.     extern char    *cmdname;
  39.  
  40.     f    = NULL;
  41.  
  42.     while ((name = *v++) != NULL)
  43.     {
  44.     if (stat(name, &s) == 0)
  45.     {
  46.         switch (s.st_mode & S_IFMT)
  47.         {
  48.         case S_IFREG:
  49.             f = add_file(name, f);
  50.             break;
  51.         case S_IFDIR:
  52.             f = add_dir(name, f);
  53.             break;
  54.         default:
  55.             break;
  56.         }
  57.     }
  58.     else
  59.     {
  60.         fprintf(stderr, "%s: warning - can't stat %s", cmdname, name);
  61.     }
  62.     }
  63.  
  64.     if (f != NULL)
  65.     while (f->prev)
  66.         f = f->prev;
  67.  
  68.     return f;
  69. }
  70.  
  71. STATIC list_t *
  72. add_file(
  73.     char    *name,
  74.     list_t    *p
  75.     )
  76. {
  77.     list_t    *f;
  78.     char    *n;
  79.  
  80.     if ( (f = (list_t *) malloc(sizeof(list_t))) == NULL)
  81.     return p;
  82.  
  83.     if ((n = strdup(name)) == NULL)
  84.     return p;
  85.  
  86.     if (p)
  87.         p->next    = f;
  88.     f->prev    = p;
  89.     f->next    = NULL;
  90.     f->name    = n;
  91.     f->flags    = 0;
  92.  
  93.     return    f;
  94. }
  95.  
  96. /*ARGSUSED*/
  97. STATIC list_t *
  98. add_dir(
  99.     char    *name,
  100.     list_t    *p
  101.     )
  102. {
  103.     return p;
  104. }
  105.  
  106. /*
  107.  * fileListSort()    - simple insertion sort for file lists
  108.  */
  109. list_t    *
  110. fileListSort(
  111.     list_t    *f
  112.     )
  113. {
  114.     list_t    *p;
  115.     list_t    first;
  116.  
  117.     /*
  118.      * set up sentinel element at start of list
  119.      */
  120.     first.prev    = NULL;
  121.     first.next    = f;
  122.     first.name    = "";
  123.     f->prev    = &first;
  124.  
  125.     /*
  126.      * sort
  127.      */
  128.     for (f = f->next; f != NULL; f = f->next)
  129.     {
  130.     p = f->prev;
  131.     while (strcmp(f->name, p->name) < 0)
  132.         p = p->prev;
  133.  
  134.     if (p != f->prev)
  135.     {
  136.         /*
  137.          * remove f from current place in list
  138.          */
  139.         if (f->next)
  140.         f->next->prev    = f->prev;
  141.         f->prev->next    = f->next;
  142.  
  143.         /*
  144.          * insert f into list following p
  145.          */
  146.         f->next        = p->next;
  147.         f->next->prev    = f;
  148.         p->next        = f;
  149.         f->prev        = p;
  150.     }
  151.     }
  152.  
  153.     /*
  154.      * remove duplicates
  155.      */
  156.     for (f = first.next; f != NULL; f = f->next)
  157.     {
  158.     while (f->next != NULL && strcmp(f->name, f->next->name) == 0)
  159.     {
  160.         p        = f->next;
  161.         f->next    = p->next;
  162.         if (f->next)
  163.         f->next->prev = f;
  164.         free(p->name);
  165.         free(p);
  166.     }
  167.     }
  168.  
  169.     /*
  170.      * return pointer to list element following the sentinel
  171.      */
  172.     f        = first.next;
  173.     f->prev    = NULL;
  174.  
  175.     return    f;
  176. }
  177.  
  178.